import http.client,unittest,HTMLTestRunner,pymysql,json,re

class http_test(unittest.TestCase):
    def test_get(self):
        #建立连接
        con=http.client.HTTPConnection(host='localhost',port=80)
        #发送GET请求
        con.request(method='GET',url='/agileone/')
        #获取响应
        res=con.getresponse().read().decode()
        print(res)
        #响应断言
        self.assertIn('AgileOne - Welcome to Login',res)
        con.close()
    def test_post(self):
        # 建立连接
        con = http.client.HTTPConnection(host='localhost', port=80)
        #发送POST请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type':' application/x-www-form-urlencoded'})
        #获取响应
        res =con.getresponse().read().decode()
        #获取响应头信息
        # headers=con.getresponse().getheaders()
        # print(headers)
        # 依据响应头名称
        header=con.getresponse().getheader('Set-Cookie')
        print(header)
        #响应断言
        self.assertEqual(res,'successful')
        con.close()
    def test_add(self):
        ###登录
        con = http.client.HTTPConnection('localhost', 80)
        con.request(method='POST', url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type': ' application/x-www-form-urlencoded'})
        cookie=con.getresponse().getheader('Set-Cookie')
        print(cookie.split(';')[0])
        cookie_1=cookie.split(';')[0]
        ###新增公告
        con=http.client.HTTPConnection('localhost',80)
        con.request('POST','/agileone/index.php/notice/add',
                    'headline=asdaasdasdas&content=&scope=1&expireddate=2019-02-06',
                    {'Content-Type': ' application/x-www-form-urlencoded','Cookie':cookie_1})
        res=con.getresponse().read().decode()
        print(res)
        #响应断言：self.assertGreater;数据库查询;功能交互查询结果;正则表达式

        self.assertGreater(int(res),1)
        # #########################连接数据库
        sql_con=pymysql.Connect(host='localhost',user='root',
                                password='123456',database='agileone',
                                charset='utf8')
        #创建游标
        cusor=sql_con.cursor()
        #执行查询SQL
        sql='SELECT noticeid FROM notice WHERE noticeid=%s'%res
        cusor.execute(sql)
        #获取查询结果
        result=cusor.fetchone()
        result1=result[0]
        print(result1)
        #关闭数据库连接
        cusor.close()
        self.assertEqual(result1,int(res))
    ######################搜索，查询ID是否符合预期
        con_query=http.client.HTTPConnection('localhost',80)
        con_query.request('POST','/agileone/index.php/notice/query',
                          'currentpage=1&noticeid=%s'%res,
                          {'Content-Type': ' application/x-www-form-urlencoded','Cookie':cookie_1})
        res_query=con_query.getresponse().read()
        print(res_query)
        #res_py=json.loads(res_query)
        #noticeid=res_py[0]['noticeid']
        #headline=res_py[0]['headline']
        #self.assertEqual(noticeid,res)
        #self.assertEqual(headline,'asdaasdasdas')
        #######正则表达式断言#####  ^\d{n}$
        #pattern="^\d{3}$"
        #self.assertRegex(res,pattern)


if __name__ == '__main__':
    #http_test().test_add()
    # unittest.main()
    suite=unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(http_test))
    with open('http_测试报告2.html','w',encoding='utf-8') as file:
        HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,
                                      title='测试报告',description='HTTP测试报告').run(suite)


